﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using AnyOffice.Components;
using AnyOffice.DAL;
using FunkeLab;
namespace AnyOffice.Control
{   
    public  partial class DepartmentAndUsersControl : UserControl
    {
        private string _target = "main";
        private string _navigateUrl = "user.aspx";
        private MyTreeViewItem treeViewItem;
        private SqlConnection conn;
        private MyTreeViewItem item;

        public string NavigateUrl
        {
            get
            {
                return this._navigateUrl;
            }
            set
            {
                this._navigateUrl = value;
            }
        }


        public string Target
        {
            get
            {
                return this._target;
            }
            set
            {
                this._target = value;
            }
        }
        private void GetDept(ref MyTreeViewItem nodeItem, int deptId)
        {
            DataTable table = new DepartmentDao().SelectChildAll(this.conn, deptId);
            UsersDao dao2 = new UsersDao();
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    MyTreeViewItem item = new MyTreeViewItem();
                    item.Text = table.Rows[i]["DeptName"].ToString();
                    item.Image = "/images/node_dept.gif";
                    DataTable table2 = dao2.SelectBaseByDeptId(this.conn, Convert.ToInt32(table.Rows[i]["DeptId"].ToString()));
                    if (table2.Rows.Count > 0)
                    {
                        for (int j = 0; j < table2.Rows.Count; j++)
                        {
                            this.item = new MyTreeViewItem();
                            this.item.Text = table2.Rows[j]["UserName"].ToString();
                            this.item.Image = "/images/node_user.gif";
                            this.item.Link = this._navigateUrl + "?user_id=" + table2.Rows[j]["UserId"].ToString() + "&user_name=" + Server.UrlEncode(table2.Rows[j]["UserName"].ToString());
                            this.item.TargetFrame = this._target;
                            item.Items.Add(this.item);
                        }
                    }
                    this.GetDept(ref item, int.Parse(table.Rows[i]["DeptId"].ToString()));
                    nodeItem.Items.Add(item);
                }
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.conn = new SqlConnection(ConfigManager.ConnectionString);
                DataTable table = new DepartmentDao().SelectParentAll(this.conn);
                UsersDao dao2 = new UsersDao();
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        this.treeViewItem = new MyTreeViewItem();
                        this.treeViewItem.Text = table.Rows[i]["DeptName"].ToString();
                        this.treeViewItem.Image = "/images/node_dept.gif";
                        DataTable table2 = dao2.SelectBaseByDeptId(this.conn, Convert.ToInt32(table.Rows[i]["DeptId"].ToString()));
                        if (table2.Rows.Count > 0)
                        {
                            for (int j = 0; j < table2.Rows.Count; j++)
                            {
                                this.item = new MyTreeViewItem();
                                this.item.Text = table2.Rows[j]["UserName"].ToString();
                                this.item.Image = "/images/node_user.gif";
                                this.item.Link = this._navigateUrl + "?user_id=" + table2.Rows[j]["UserId"].ToString() + "&user_name=" + Server.UrlEncode(table2.Rows[j]["UserName"].ToString());
                                this.item.TargetFrame = this._target;
                                this.treeViewItem.Items.Add(this.item);
                            }
                        }
                        this.GetDept(ref this.treeViewItem, int.Parse(table.Rows[i]["DeptId"].ToString()));
                        this.TreeView1.Items.Add(this.treeViewItem);
                    }
                }
                if (this.conn.State == ConnectionState.Open)
                {
                    this.conn.Close();
                }
            }
        }

    }
}

